package com.briup.srms.sms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.srms.sms.bean.Direction;
import com.briup.srms.sms.bean.DirectionExample;
import com.briup.srms.sms.bean.extend.DirectionExtend;
import com.briup.srms.sms.dao.DirectionMapper;
import com.briup.srms.sms.dao.extend.DirectionExtendMapper;
import com.briup.srms.sms.service.IDirectionService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class DirectionServiceImpl implements IDirectionService {
	
	@Autowired
	private DirectionMapper directionMapper;
	@Autowired
	private DirectionExtendMapper directionExtendMapper;
	@Override
	
	public List<DirectionExtend> queryDirectionClazzTree() {
		return directionExtendMapper.queryDirectionClazzTree();
	}
	
	@Override
	public PageVM<Direction> pageQuery(int page, int pageSize) {
		List<Direction> list = directionExtendMapper.query(page, pageSize);
		long total = directionExtendMapper.count();
		return new PageVM<>(page, pageSize, total, list);
	}
	@Override
	public void saveOrUpdate(Direction direction) {
		if(direction.getId() == null) {
			directionMapper.insert(direction);
		} else {
			directionMapper.updateByPrimaryKey(direction);
		}
		
	}
	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		Direction dict = directionMapper.selectByPrimaryKey(id);
		if(dict == null) {
			throw new CustomerException("要删除的数据不存在");
		}
		directionMapper.deleteByPrimaryKey(id);
		
	}

	@Override
	public Direction queryByName(String name) {
		DirectionExample example = new DirectionExample();
		example.createCriteria().andNameEqualTo(name);
		List<Direction> list = directionMapper.selectByExample(example);
		if(list.size()>0) {
			return list.get(0);
		}
		return null;
	}
}
